using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using AleaClasses;
using System.Xml;
using EnablementManager;

namespace AleaDesigner
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class TfrmMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ToolBar tbMain;
		private System.Windows.Forms.ToolBarButton btnSave;
		private System.Windows.Forms.ToolBarButton sep1;
		private System.Windows.Forms.ToolBarButton btnExit;
		private System.Windows.Forms.ImageList imglMain;
		private System.Windows.Forms.MainMenu menuMain;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem mniNew;
		private System.Windows.Forms.MenuItem mniOpen;
		private System.Windows.Forms.MenuItem mniSave;
		private System.Windows.Forms.MenuItem mniSaveAs;
		private System.Windows.Forms.MenuItem mniExit;
		private System.Windows.Forms.MenuItem mniAbout;
		private System.Windows.Forms.MenuItem mniClose;
		private System.Windows.Forms.ToolBarButton btnNew;
		private System.Windows.Forms.ToolBarButton btnSaveAs;
		private System.Windows.Forms.ToolBarButton btnClose;
		private System.Windows.Forms.ToolBarButton btnOpen;
		private System.Windows.Forms.OpenFileDialog odAleaGraph;
		private System.Windows.Forms.SaveFileDialog sdAleaGraph;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem mniLabels;
		private System.Windows.Forms.MenuItem mniNodes;
		private System.Windows.Forms.Panel pnlMainBase;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.StatusBar sbMain;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label lblSubFormName;
		private System.Windows.Forms.Panel pnlMain;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem mniAddLabel;
		private System.Windows.Forms.MenuItem mniDeleteLabel;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem mniAddNode;
		private System.Windows.Forms.MenuItem mniDeleteNode;
		private System.Windows.Forms.ToolBarButton btnLabels;
		private System.Windows.Forms.ToolBarButton btnNodes;
		private System.Windows.Forms.ToolBarButton sep2;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem mniAddEdge;
		private System.Windows.Forms.MenuItem mniDeleteEdge;
		private System.Windows.Forms.MenuItem mniAddArrivalLabel;
		private System.Windows.Forms.MenuItem mniDeleteArrivalLabel;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem mniPlay;
		private System.ComponentModel.IContainer components;

		public TfrmMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.tbMain = new System.Windows.Forms.ToolBar();
			this.btnNew = new System.Windows.Forms.ToolBarButton();
			this.btnOpen = new System.Windows.Forms.ToolBarButton();
			this.btnSave = new System.Windows.Forms.ToolBarButton();
			this.btnSaveAs = new System.Windows.Forms.ToolBarButton();
			this.btnClose = new System.Windows.Forms.ToolBarButton();
			this.sep1 = new System.Windows.Forms.ToolBarButton();
			this.btnLabels = new System.Windows.Forms.ToolBarButton();
			this.btnNodes = new System.Windows.Forms.ToolBarButton();
			this.sep2 = new System.Windows.Forms.ToolBarButton();
			this.btnExit = new System.Windows.Forms.ToolBarButton();
			this.imglMain = new System.Windows.Forms.ImageList(this.components);
			this.pnlMainBase = new System.Windows.Forms.Panel();
			this.pnlMain = new System.Windows.Forms.Panel();
			this.panel1 = new System.Windows.Forms.Panel();
			this.lblSubFormName = new System.Windows.Forms.Label();
			this.menuMain = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.mniNew = new System.Windows.Forms.MenuItem();
			this.mniOpen = new System.Windows.Forms.MenuItem();
			this.mniSave = new System.Windows.Forms.MenuItem();
			this.mniSaveAs = new System.Windows.Forms.MenuItem();
			this.mniClose = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.mniExit = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.mniLabels = new System.Windows.Forms.MenuItem();
			this.mniNodes = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.mniAddLabel = new System.Windows.Forms.MenuItem();
			this.mniDeleteLabel = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.mniAddNode = new System.Windows.Forms.MenuItem();
			this.mniDeleteNode = new System.Windows.Forms.MenuItem();
			this.mniAddArrivalLabel = new System.Windows.Forms.MenuItem();
			this.mniDeleteArrivalLabel = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.mniAddEdge = new System.Windows.Forms.MenuItem();
			this.mniDeleteEdge = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.mniPlay = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.mniAbout = new System.Windows.Forms.MenuItem();
			this.odAleaGraph = new System.Windows.Forms.OpenFileDialog();
			this.sdAleaGraph = new System.Windows.Forms.SaveFileDialog();
			this.panel2 = new System.Windows.Forms.Panel();
			this.sbMain = new System.Windows.Forms.StatusBar();
			this.pnlMainBase.SuspendLayout();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// tbMain
			// 
			this.tbMain.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					  this.btnNew,
																					  this.btnOpen,
																					  this.btnSave,
																					  this.btnSaveAs,
																					  this.btnClose,
																					  this.sep1,
																					  this.btnLabels,
																					  this.btnNodes,
																					  this.sep2,
																					  this.btnExit});
			this.tbMain.DropDownArrows = true;
			this.tbMain.ImageList = this.imglMain;
			this.tbMain.Location = new System.Drawing.Point(0, 0);
			this.tbMain.Name = "tbMain";
			this.tbMain.ShowToolTips = true;
			this.tbMain.Size = new System.Drawing.Size(672, 42);
			this.tbMain.TabIndex = 0;
			// 
			// btnNew
			// 
			this.btnNew.Tag = "New";
			this.btnNew.Text = "New";
			// 
			// btnOpen
			// 
			this.btnOpen.Tag = "Open";
			this.btnOpen.Text = "Open";
			// 
			// btnSave
			// 
			this.btnSave.Tag = "Save";
			this.btnSave.Text = "Save";
			// 
			// btnSaveAs
			// 
			this.btnSaveAs.Tag = "Save As";
			this.btnSaveAs.Text = "SaveAs";
			// 
			// btnClose
			// 
			this.btnClose.Tag = "Close";
			this.btnClose.Text = "Close";
			// 
			// sep1
			// 
			this.sep1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// btnLabels
			// 
			this.btnLabels.Tag = "View Labels";
			this.btnLabels.Text = "Labels";
			// 
			// btnNodes
			// 
			this.btnNodes.Tag = "View Nodes";
			this.btnNodes.Text = "Nodes";
			// 
			// sep2
			// 
			this.sep2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// btnExit
			// 
			this.btnExit.Tag = "Exit";
			this.btnExit.Text = "Exit";
			// 
			// imglMain
			// 
			this.imglMain.ImageSize = new System.Drawing.Size(16, 16);
			this.imglMain.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// pnlMainBase
			// 
			this.pnlMainBase.Controls.Add(this.pnlMain);
			this.pnlMainBase.Controls.Add(this.panel1);
			this.pnlMainBase.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlMainBase.Location = new System.Drawing.Point(0, 42);
			this.pnlMainBase.Name = "pnlMainBase";
			this.pnlMainBase.Size = new System.Drawing.Size(672, 349);
			this.pnlMainBase.TabIndex = 1;
			// 
			// pnlMain
			// 
			this.pnlMain.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlMain.Location = new System.Drawing.Point(0, 30);
			this.pnlMain.Name = "pnlMain";
			this.pnlMain.Size = new System.Drawing.Size(672, 319);
			this.pnlMain.TabIndex = 5;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.lblSubFormName);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(672, 30);
			this.panel1.TabIndex = 4;
			// 
			// lblSubFormName
			// 
			this.lblSubFormName.BackColor = System.Drawing.Color.Purple;
			this.lblSubFormName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lblSubFormName.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lblSubFormName.Font = new System.Drawing.Font("Tahoma", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblSubFormName.ForeColor = System.Drawing.Color.White;
			this.lblSubFormName.Location = new System.Drawing.Point(0, 0);
			this.lblSubFormName.Name = "lblSubFormName";
			this.lblSubFormName.Size = new System.Drawing.Size(672, 30);
			this.lblSubFormName.TabIndex = 0;
			// 
			// menuMain
			// 
			this.menuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem1,
																					 this.menuItem2,
																					 this.menuItem3,
																					 this.menuItem4,
																					 this.menuItem5,
																					 this.menuItem7,
																					 this.menuItem8});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniNew,
																					  this.mniOpen,
																					  this.mniSave,
																					  this.mniSaveAs,
																					  this.mniClose,
																					  this.menuItem6,
																					  this.mniExit});
			this.menuItem1.Text = "&File";
			// 
			// mniNew
			// 
			this.mniNew.Index = 0;
			this.mniNew.Text = "New";
			// 
			// mniOpen
			// 
			this.mniOpen.Index = 1;
			this.mniOpen.Text = "Open";
			// 
			// mniSave
			// 
			this.mniSave.Index = 2;
			this.mniSave.Text = "Save";
			// 
			// mniSaveAs
			// 
			this.mniSaveAs.Index = 3;
			this.mniSaveAs.Text = "Save As";
			// 
			// mniClose
			// 
			this.mniClose.Index = 4;
			this.mniClose.Text = "Close";
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 5;
			this.menuItem6.Text = "-";
			// 
			// mniExit
			// 
			this.mniExit.Index = 6;
			this.mniExit.Text = "Exit";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniLabels,
																					  this.mniNodes});
			this.menuItem2.Text = "&View";
			// 
			// mniLabels
			// 
			this.mniLabels.Index = 0;
			this.mniLabels.Text = "View Labels";
			// 
			// mniNodes
			// 
			this.mniNodes.Index = 1;
			this.mniNodes.Text = "View Nodes";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 2;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniAddLabel,
																					  this.mniDeleteLabel});
			this.menuItem3.Text = "&Labels";
			// 
			// mniAddLabel
			// 
			this.mniAddLabel.Index = 0;
			this.mniAddLabel.Text = "Add Label";
			// 
			// mniDeleteLabel
			// 
			this.mniDeleteLabel.Index = 1;
			this.mniDeleteLabel.Text = "Delete Label";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 3;
			this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniAddNode,
																					  this.mniDeleteNode,
																					  this.mniAddArrivalLabel,
																					  this.mniDeleteArrivalLabel});
			this.menuItem4.Text = "&Nodes";
			// 
			// mniAddNode
			// 
			this.mniAddNode.Index = 0;
			this.mniAddNode.Text = "Add Node";
			// 
			// mniDeleteNode
			// 
			this.mniDeleteNode.Index = 1;
			this.mniDeleteNode.Text = "Delete Node";
			// 
			// mniAddArrivalLabel
			// 
			this.mniAddArrivalLabel.Index = 2;
			this.mniAddArrivalLabel.Text = "Add Arrival Label";
			// 
			// mniDeleteArrivalLabel
			// 
			this.mniDeleteArrivalLabel.Index = 3;
			this.mniDeleteArrivalLabel.Text = "Delete Arrival Label";
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 4;
			this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniAddEdge,
																					  this.mniDeleteEdge});
			this.menuItem5.Text = "&Edges";
			// 
			// mniAddEdge
			// 
			this.mniAddEdge.Index = 0;
			this.mniAddEdge.Text = "Add Edge";
			// 
			// mniDeleteEdge
			// 
			this.mniDeleteEdge.Index = 1;
			this.mniDeleteEdge.Text = "Delete Edge";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 5;
			this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniPlay});
			this.menuItem7.Text = "&Action";
			// 
			// mniPlay
			// 
			this.mniPlay.Index = 0;
			this.mniPlay.Text = "Play";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 6;
			this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mniAbout});
			this.menuItem8.Text = "&Help";
			// 
			// mniAbout
			// 
			this.mniAbout.Index = 0;
			this.mniAbout.Text = "&About";
			this.mniAbout.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// odAleaGraph
			// 
			this.odAleaGraph.DefaultExt = "AleaGraph.xml";
			this.odAleaGraph.Filter = "Alea Graphs|*.AleaGraph.xml|All Files|*.*";
			// 
			// sdAleaGraph
			// 
			this.sdAleaGraph.DefaultExt = "AleaGraph.xml";
			this.sdAleaGraph.Filter = "Alea Graphs|*.AleaGraph.xml|All Files|*.*";
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.sbMain);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 391);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(672, 32);
			this.panel2.TabIndex = 5;
			// 
			// sbMain
			// 
			this.sbMain.Dock = System.Windows.Forms.DockStyle.Fill;
			this.sbMain.Location = new System.Drawing.Point(0, 0);
			this.sbMain.Name = "sbMain";
			this.sbMain.Size = new System.Drawing.Size(672, 32);
			this.sbMain.TabIndex = 6;
			// 
			// TfrmMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(672, 423);
			this.Controls.Add(this.pnlMainBase);
			this.Controls.Add(this.tbMain);
			this.Controls.Add(this.panel2);
			this.Menu = this.menuMain;
			this.MinimumSize = new System.Drawing.Size(680, 450);
			this.Name = "TfrmMain";
			this.Text = "Alea Designer 1.0 by Emlyn O\'Regan 2006";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.TfrmMain_Closing);
			this.Load += new System.EventHandler(this.TfrmMain_Load);
			this.pnlMainBase.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new TfrmMain());
		}

		private void menuItem9_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Alea v1.0" + Environment.NewLine + "(c) Emlyn O'Regan 2006" + Environment.NewLine + "EmlynORegan.com", "Alea");
		}

		private TEnablementManager fEnablementManager = new TEnablementManager();
		private TAleaGraph fAleaGraph = null;
		private string fFileName = null;
		private Form fSubForm = null;

		public Form SubForm
		{
			get
			{
				return fSubForm;
			}
			set
			{
				if (fSubForm != value)
				{
					if (fSubForm != null)
					{
						fSubForm.Hide();
						fSubForm.Close();
						fSubForm = null;
					}
					fSubForm = value;
				}
			}
		}

		private TfrmLabels LabelsForm
		{
			get
			{
				if ((SubForm != null) && (SubForm is TfrmLabels))
					return SubForm as TfrmLabels;
				else
					return null;
			}
		}

		private TfrmNodes NodesForm
		{
			get
			{
				if ((SubForm != null) && (SubForm is TfrmNodes))
					return SubForm as TfrmNodes;
				else
					return null;
			}
		}

		public void UpdateEnablement()
		{
			if (fAleaGraph == null)
				sbMain.Text = "No AleaGraph.xml Document Open";
			else if (fFileName == null)
				sbMain.Text = "A new AleaGraph.xml Document is Open";
			else 
				sbMain.Text = fFileName;

			fEnablementManager.ProcessEnablement();

			UpdateSubForm();
		}

		private bool CheckSaveChanges()
		{
			bool retval = true;

			if ((fAleaGraph != null) && (fAleaGraph.NeedsSave()))
			{
				DialogResult dr = 
					MessageBox.Show(
					"Do you wish to save your changes?", 
					"Alea - Save Changes",
					MessageBoxButtons.YesNoCancel
					);

				if (dr == DialogResult.Yes)
				{
					DoSave();
					retval = NeedsSave();
				}
				else if (dr == DialogResult.No)
				{
					DoCloseNoDialog();
					retval = NeedsSave();
				}
				else if (dr == DialogResult.Cancel)
				{
					retval = false;
				}
			}

			return retval;
		}

		private bool NeedsSave()
		{
			return (fAleaGraph != null) && (fAleaGraph.NeedsSave());
		}

		private void TfrmMain_Load(object sender, System.EventArgs e)
		{
			TEnablement Enablement = null;

			//
			// First do main form enablements
			//
			Enablement = fEnablementManager.CreateEnablement(
				"New", 
				new TEnablementCheckDelegate(NewEnablementCheck),
				new TEnablementExecuteDelegate(DoNew),
				"&New"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Open", 
				new TEnablementCheckDelegate(OpenEnablementCheck),
				new TEnablementExecuteDelegate(DoOpen),
				"&Open"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Save", 
				new TEnablementCheckDelegate(SaveEnablementCheck),
				new TEnablementExecuteDelegate(DoSave),
				"&Save"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Save As", 
				new TEnablementCheckDelegate(SaveAsEnablementCheck),
				new TEnablementExecuteDelegate(DoSaveAs),
				"&Save As"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Close", 
				new TEnablementCheckDelegate(CloseEnablementCheck),
				new TEnablementExecuteDelegate(DoClose),
				"&Close"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Exit", 
				new TEnablementCheckDelegate(ExitEnablementCheck),
				new TEnablementExecuteDelegate(DoExit),
				"E&xit"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"View Labels", 
				new TEnablementCheckDelegate(ViewLabelsEnablementCheck),
				new TEnablementExecuteDelegate(DoViewLabels),
				"&Labels"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"View Nodes", 
				new TEnablementCheckDelegate(ViewNodesEnablementCheck),
				new TEnablementExecuteDelegate(DoViewNodes),
				"&Nodes"
				);

			Enablement = fEnablementManager.CreateEnablement(
				"Play", 
				new TEnablementCheckDelegate(PlayEnablementCheck),
				new TEnablementExecuteDelegate(DoPlay),
				"&Play"
				);

			//
			// Now create placeholders for other forms
			//
			Enablement = fEnablementManager.CreatePlaceholderEnablement("Add Label", "&Add");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Delete Label", "&Delete");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Add Node", "&Add Node");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Delete Node", "&Delete Node");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Add Arrival Label", "Add A&rrival Label");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Delete Arrival Label", "Delete Arr&ival Label");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Add Edge", "&Add");

			Enablement = fEnablementManager.CreatePlaceholderEnablement("Delete Edge", "&Delete");

			//
			// Now add the menu and toolbar to all relevant enablements
			//
			
			fEnablementManager.AddMenu(menuMain);
			fEnablementManager.AddToolbar(tbMain);

			UpdateEnablement();
		}

		private void OpenEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = true;
		}

		private void SaveEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = (fFileName != null) & NeedsSave();
		}

		private void SaveAsEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = (fAleaGraph != null);
		}

		private void NewEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = true;
		}

		private void CloseEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = fAleaGraph != null;
		}

		private void ExitEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = true;
		}

		private void ViewLabelsEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = fAleaGraph != null;
		}

		private void ViewNodesEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = fAleaGraph != null;
		}

		private void PlayEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = (fAleaGraph != null);
		}

		private void DoNew()
		{
			CheckSaveChanges();

			if (!NeedsSave())
			{
				fAleaGraph = new TAleaGraph();
				fAleaGraph.ClearNeedsSave();
				fFileName = null;
				DoViewNodes();
			}
		}

		private void DoOpen()
		{
			CheckSaveChanges();

			if (!NeedsSave())
			{
				if (odAleaGraph.ShowDialog() == DialogResult.OK)
				{
					XmlDocument lDoc = new XmlDocument();
					lDoc.Load(odAleaGraph.FileName);
					fAleaGraph = new TAleaGraph(lDoc.ChildNodes[0] as XmlElement);
					fFileName = odAleaGraph.FileName;
					DoViewNodes();
				}
			}
		}

		private void DoSave()
		{
			if (fFileName == null)
			{
				DoSaveAs();
			}
			else
			{
				XmlDocument lDoc = new XmlDocument();
				XmlElement rootelem = fAleaGraph.SaveToXml(lDoc);
				lDoc.AppendChild(rootelem);
				lDoc.Save(fFileName);
				fAleaGraph.ClearNeedsSave();
				UpdateEnablement();
			}
		}

		private void DoSaveAs()
		{
			if (sdAleaGraph.ShowDialog() == DialogResult.OK)
			{
				XmlDocument lDoc = new XmlDocument();
				XmlElement rootelem = fAleaGraph.SaveToXml(lDoc);
				lDoc.AppendChild(rootelem);
				fFileName = sdAleaGraph.FileName;
				lDoc.Save(fFileName);
				fAleaGraph.ClearNeedsSave();
				UpdateEnablement();
			}
		}

		private void DoClose()
		{
			CheckSaveChanges();

			if (!NeedsSave())
			{
				DoCloseNoDialog();
			}
		}

		private void DoCloseNoDialog()
		{
			SubForm = null;
			fAleaGraph = null;
			fFileName = null;
			UpdateEnablement();
		}

		private void DoExit()
		{
			Close();
		}

		private void DoViewLabels()
		{
			TfrmLabels lLabelsForm = new TfrmLabels();
			lLabelsForm.AleaGraph = fAleaGraph;
			lLabelsForm.SignalChangeDelegate = new TSignalChangeDelegate(OnSignalChange);
			lLabelsForm.TopLevel = false;
			lLabelsForm.FormBorderStyle = FormBorderStyle.None;
			lLabelsForm.Parent = pnlMain;
			lLabelsForm.Dock = DockStyle.Fill;
			lLabelsForm.EnablementManager = fEnablementManager;
			SubForm = lLabelsForm;
			lLabelsForm.Show();

			UpdateEnablement();
		}

		private void DoViewNodes()
		{
			TfrmNodes lNodesForm = new TfrmNodes();
			lNodesForm.AleaGraph = fAleaGraph;
			lNodesForm.SignalChangeDelegate = new TSignalChangeDelegate(OnSignalChange);
			lNodesForm.TopLevel = false;
			lNodesForm.FormBorderStyle = FormBorderStyle.None;
			lNodesForm.Parent = pnlMain;
			lNodesForm.Dock = DockStyle.Fill;
			lNodesForm.EnablementManager = fEnablementManager;
			SubForm = lNodesForm;
			lNodesForm.Show();

			UpdateEnablement();
		}

		private void DoPlay()
		{
			TfrmPlayer lPlayer = new TfrmPlayer();
			lPlayer.AleaGraph = fAleaGraph;

			lPlayer.ShowDialog();
		}
	
		private void UpdateSubForm()
		{
			if (SubForm == null)
			{
				lblSubFormName.Visible = false;
			}
			else
			{
				lblSubFormName.Visible = true;
				lblSubFormName.Text = SubForm.Text;
			}
		}


//		private void mniNew_Click(object sender, System.EventArgs e)
//		{
//			DoNew();
//		}
//
//		private void mniOpen_Click(object sender, System.EventArgs e)
//		{
//			DoOpen();
//		}
//
//		private void mniSave_Click(object sender, System.EventArgs e)
//		{
//			DoSave();
//		}
//
//		private void mniSaveAs_Click(object sender, System.EventArgs e)
//		{
//			DoSaveAs();
//		}
//
//		private void mniClose_Click(object sender, System.EventArgs e)
//		{
//			DoClose();
//		}
//
//		private void mniExit_Click(object sender, System.EventArgs e)
//		{
//			DoExit();
//		}
//
//		private void mniLabels_Click(object sender, System.EventArgs e)
//		{
//			DoViewLabels();
//		}

//		private void tbMain_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
//		{
//			if (e.Button == btnNew)
//				fEnablementManager.Enablements.GetEnablementByName("New").Execute();
//			else if (e.Button == btnOpen)
//				fEnablementManager.Enablements.GetEnablementByName("Open").Execute();
//			else if (e.Button == btnSave)
//				fEnablementManager.Enablements.GetEnablementByName("Save").Execute();
//			else if (e.Button == btnSaveAs)
//				fEnablementManager.Enablements.GetEnablementByName("Save As").Execute();
//			else if (e.Button == btnClose)
//				fEnablementManager.Enablements.GetEnablementByName("Close").Execute();
//			else if (e.Button == btnLabels)
//				fEnablementManager.Enablements.GetEnablementByName("View Labels").Execute();
//			else if (e.Button == btnNodes)
//				fEnablementManager.Enablements.GetEnablementByName("View Nodes").Execute();
//			else if (e.Button == btnExit)
//				fEnablementManager.Enablements.GetEnablementByName("Exit").Execute();
//		}

		private void TfrmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			CheckSaveChanges();

			e.Cancel = NeedsSave();
		}

		public void OnSignalChange()
		{
			UpdateEnablement();
		}

//		private void mniAddLabel_Click(object sender, System.EventArgs e)
//		{
//			LabelsForm.DoAddLabel();
//		}
//
//		private void mniDeleteLabel_Click(object sender, System.EventArgs e)
//		{
//			LabelsForm.DoDeleteLabel();
//		}
//
//		private void mniNodesAdd_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoAddNode();
//		}
//
//		private void NodesDelete_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoDeleteNode();
//		}
//
//		private void mniNodes_Click(object sender, System.EventArgs e)
//		{
//			DoViewNodes();
//		}
//
//		private void mniAddEdge_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoAddEdge();
//		}
//
//		private void mniDeleteEdge_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoDeleteEdge();
//		}
//
//		private void mniAddArrivalLabel_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoAddArrivalLabel();
//		}
//
//		private void mniDeleteArrivalLabel_Click(object sender, System.EventArgs e)
//		{
//			NodesForm.DoDeleteArrivalLabel();
//		}
//
//		private void mniPlay_Click(object sender, System.EventArgs e)
//		{
//			DoPlay();
//		}
	}

	public delegate void TSignalChangeDelegate();
}
